package com.tju.welding.controller;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.tju.welding.service.TestService;
import com.tju.welding.utils.ApiResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("/connection")
    public String testConnection() {

        return "connection ok";

    }

    @GetMapping("/image")
    public byte[] getImage(int data) throws FileNotFoundException, IOException {

        return testService.getImage(data); 

    }

    @RequestMapping(value = "/image",method = RequestMethod.GET,produces = MediaType.IMAGE_PNG_VALUE)
    @ResponseBody
    private byte[] getPic(@RequestParam int data) throws IOException {

        return testService.getImage(data);
        
    }

    @PostMapping("/record")
    public ApiResponse addRecord(@RequestParam("data") int data, @RequestParam("image") MultipartFile image) {

        return testService.addRecord(data, image);
    
    }

}
